<?php


namespace common\services;

use sdk\alipay\aop\AopClient;
use sdk\alipay\aop\request\AlipayFundTransToaccountTransferRequest;
use sdk\alipay\aop\request\AlipayTradeAppPayRequest;
use sdk\alipay\aop\request\AlipayTradePagePayRequest;
use sdk\alipay\aop\request\AlipayTradeWapPayRequest;

use common\helpers\Util;

class AlipayService
{

    public function createSign($body, $subject, $out_trade_no, $amount, $order_id = null)
    {
        $aop = new AopClient();
        $aop->gatewayUrl = \Yii::$app->params['ali_conf']['alipay_gateway'];
        $aop->appId = \Yii::$app->params['ali_conf']['alipay_app_id'];
        $aop->rsaPrivateKey = \Yii::$app->params['ali_conf']['alipay_private_key'];
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = \Yii::$app->params['ali_conf']['alipay_public_key'];
        $request = new AlipayTradeAppPayRequest();
        $timeoutExpress = \Yii::$app->params['ali_conf']['alipay_timeout_express'];
        $bizcontent = "{\"body\":\"{$body}\","
            . "\"subject\": \"{$subject}\","
            . "\"out_trade_no\":\"{$out_trade_no}\","
            . "\"timeout_express\": \"{$timeoutExpress}m\","
            . "\"total_amount\":\"{$amount}\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\",";
        if (!empty($order_id)) {
            $params = urlencode("order_id={$order_id}");
            $bizcontent .= "\"passback_params\":\"{$params}\",";
        }
        $bizcontent = rtrim($bizcontent, ',');
        $bizcontent .= "}";
        $request->setNotifyUrl( \Yii::$app->params['ali_conf']['alipay_notify_url']);
        $request->setBizContent($bizcontent);
        $response = $aop->sdkExecute($request);
        return $response;
    }


}